package com.clkj.zhonghui.pojo.common;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.util.Hashtable;

/**
 * @author Yawang
 * @date 2022/11/10 9:43
 */
public class CreatPicUtil {

    public static void creatPic(String url, String id, String path,String pa) {
        {
            try {
                String text = url;
                int width = 300;
                int height = 300;
                String format = "png";
                Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
                hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
                BitMatrix bitMatrix = new MultiFormatWriter().encode(text,
                        BarcodeFormat.QR_CODE, width, height, hints);
                HttpServletRequest request = null;
//            String path = this.servletContext.getRealPath("/resource/er");
                File outputFile = new File(path + File.separator + id + ".png");
                QRUtil.writeToFile(bitMatrix, format, outputFile);

                MultipartFile cMultiFile = new MockMultipartFile("outputFile", outputFile.getName(), null, new FileInputStream(outputFile));
                String url1 = OSSUtil.uploadingImg(cMultiFile,pa);
                System.out.println(url1);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
